#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@his-pc>
#
# Distributed under terms of the MIT license.

"""

"""

class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next


class Solution:
    """
    @param l1: The first list.
    @param l2: The second list.
    @return: the sum list of l1 and l2.
    """
    def addLists2(self, l1, l2):
        n1 = self.get_num_by_list(l1)
        n2 = self.get_num_by_list(l2)

        sum = n1 + n2

        return self.make_list_by_sum(sum)

    def get_num_by_list(self, node):
        num = 0

        while node:
            num = num * 10 + node.val
            node = node.next

        return num

    def make_list_by_sum(self, num):
        # dummy_head = ListNode('dummy')
        print('make')
        assert num >= 0
        assert type(num) is int

        # 这里主要是处理 num == 0 情况
        digit, remain = num % 10, num // 10
        head = ListNode(digit)

        while remain:
            digit, remain = remain % 10, remain // 10
            # node = ListNode(digit, head)
            # head = node
            head = ListNode(digit, head)        # head = f(head)

        return head


def show(n):
    while n:
        print("%s ----> " % n.val, sep=' ', end=' ')
        n = n.next
    print()


def main():
    print("start main")

    v5 = ListNode(5)
    v4 = ListNode(4, v5)
    v3 = ListNode(1, v4)
    v2 = ListNode(2, v3)
    v1 = ListNode(2, v2)
    v0 = ListNode(2, v1)

    s = Solution()
    ret = s.addLists2(v0, v0)
    show(ret)


if __name__ == "__main__":
    main()
